//
//  NewModifyPwdController.m
//  FD
//
//  Created by xiaofang wang on 2018/11/26.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "NewModifyPwdController.h"
#import "NewModifyPwdView.h"
#import "FDSameToAndroidTextFieldCustomView.h" //2019-1-23
#import "LoginAndRegisterController.h" //登录注册功能

#import "FDPushWebSocketTool.h"
#import "FDUserTool.h"
#import "FDTabBarController.h"



@interface NewModifyPwdController ()
/**自定义view*/
@property (nonatomic, strong) NewModifyPwdView *modifypwdView;

@end

@implementation NewModifyPwdController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationItem.title = NSLocalizedString(ModifyPWD, @"修改登录密码");

    self.view.backgroundColor = [UIColor whiteColor];
    
    CGFloat x = 15;
    CGFloat y = FD_NavH;
    CGFloat w = SCREEN_WIDTH - 2*x;
    CGFloat h = 300;
    self.modifypwdView.frame = CGRectMake(x, y, w, h);
    
    [self.view addSubview:self.modifypwdView];
}


- (NewModifyPwdView *)modifypwdView {
    if (!_modifypwdView) {
        _modifypwdView = [[NewModifyPwdView alloc] init];
        
        //点击登录
        FDWeakSelf;
        
        _modifypwdView.clickLoginBlock = ^{
            [weakSelf loginAction];
        };
    }
    return _modifypwdView;
}


#pragma mark - 修改登录密码
- (void)loginAction {
    
    NSString *oldStr = self.modifypwdView.oldPwdTF.tf.text;
    NSString *xinPwdStr = self.modifypwdView.xinPwdTF.tf.text;
    NSString *sureStr = self.modifypwdView.surePwdTF.tf.text;
    
    if ([NSString isEmptyString:oldStr]) { //旧密
        [JRToast showWithText:NSLocalizedString(PleaseInPutYouOldPwd, @"请输入旧密码")];
        return;
    }
    if ([NSString isEmptyString:xinPwdStr]) {
        
        [JRToast showWithText:NSLocalizedString(Put_your_newPWd, @"请输入新密码")];
        return;
    }
    
    if ([NSString isEmptyString:sureStr]) {
        [JRToast showWithText:NSLocalizedString(Put_your_newPWd, @"请输入新密码")];
        return;
    }
    
    
    if (![xinPwdStr isEqualToString:sureStr]) {
        
        [JRToast showWithText:NSLocalizedString(ToastDissimilar, @"两次密码不一致")];
        return;
    }
    
    //调接口
    [self modifypwdOld:oldStr xin:xinPwdStr surepwd:sureStr];
}

- (void)modifypwdOld:(NSString *)old xin:(NSString *)xin surepwd:(NSString *)surepwd {
    
    [FDHttpAPi requestUserUpdatePasswordJiuPwd:old xinstr:xin surePWd:surepwd scucessBlock:^(id responseObjc) {
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {

            //不能用导航管理。有坑
//            //跳转到登录注册页面
//            LoginAndRegisterController *loginAndRegisterVC = [[LoginAndRegisterController alloc] init];
//            [self.navigationController pushViewController:loginAndRegisterVC animated:YES];
            
            //2019-2-20应该走退出登录同样逻辑
            [self skipTologinVC];
            
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
    }];
}


- (void)skipTologinVC {
    
    //断开推送websocket连接
    [[FDPushWebSocketTool defaultFdWebPushSockcet] closePushWebSocket];
    
    
    //清空导航栈 不要动画
    [self.navigationController popToRootViewControllerAnimated:NO];
    
    //清空本地数据
    [FDUserTool fd_clearUserInfo];
    
    
    //切换根视图控制器
    LoginAndRegisterController *loginVC = [[LoginAndRegisterController alloc] initWithNibName:@"LoginAndRegisterController" bundle:nil];
    UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:loginVC];
    [UIApplication sharedApplication].keyWindow.rootViewController = navVC;
    
    [self changeFDTarbarVCSelectIndex0];
}
////2019-2-15解决点击跳过导致的bug,选中首页. 不需要切换根视图控制器
- (void)changeFDTarbarVCSelectIndex0 {
    FDTabBarController *fdtabbarVC = [FDTabBarController shareFDTabarVC];
    fdtabbarVC.selectedIndex = 0;
}



@end
